var ns, WXPhotoPicker;
app = new Vue({
    el: '#app',
    data: {
        newform: {
            ID: 0,
            ServiceMan: '',//查验人
            HandleTime: '',//查验时间
            HandleContent: '',//查验情况
            action:'deviceservicecheck'
        },
        ID: 0,
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
    },
    methods: {
        do_save: function() {
            var that = this;
            if (that.newform.HandleContent == '') {
                api.toast({
                    msg: '请填写维修情况',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            } 
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                if (that.imglist[i].isupload) {
                    filelist.push(that.imglist[i].url);
                }
            }
            ns.post(that.newform, function(succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'updata_repair_con',
                        }); 
                        api.sendEvent({
                            name: 'reloadrepairlist',
                        });    
                        setTimeout(function() {
                            api.closeWin();
                        }, 300)
                        return;
                    }
                    else if (data.Msg) {
                        api.toast({
                            msg: data.Msg,
                            duration: 3000,
                            location: 'bottom'
                        });
                        return;
                    }   
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        //上传图片
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }

            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    var btn = document.querySelector('#choosedatetime');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.newform.HandleTime){
                                _self.picker.setSelectedValue(_this.newform.HandleTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.newform.WriteDate = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.newform.HandleTime){
                                _self.picker.setSelectedValue(_this.newform.HandleTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.newform.HandleTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);

                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.id || 0;
    app.newform.ID=app.ID;
    var loginusername= api.getPrefs({
        sync: true,
        key: 'loginname'
    });
    app.newform.ServiceMan=loginusername;
    app.newform.HandleTime=moment().format('YYYY/MM/DD HH:mm');
    app.initPicker();
};
